<template>
  <div class="product-detail">
    <header class="header">
      <div class="container">
        <div class="logo">
          <h1>拼多多</h1>
        </div>
        <nav class="nav">
          <router-link to="/">首页</router-link>
          <router-link to="/products">商品</router-link>
          <router-link to="/cart">购物车</router-link>
        </nav>
      </div>
    </header>

    <div class="container" v-if="product">
      <div class="detail-content">
        <div class="product-image">
          <img :src="product.image || '/placeholder.png'" :alt="product.name">
        </div>
        <div class="product-details">
          <h1>{{ product.name }}</h1>
          <p class="price">¥{{ product.price }}</p>
          <p class="description">{{ product.description }}</p>
          <p class="stock">库存: {{ product.stock }}</p>
          <div class="purchase-actions">
            <el-input-number v-model="quantity" :min="1" :max="product.stock" />
            <el-button type="primary" size="large" @click="addToCart">加入购物车</el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { useUserStore } from '../stores'
import request from '../utils/request'

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
const product = ref(null)
const quantity = ref(1)

const addToCart = async () => {
  if (!userStore.user || !userStore.user.id) {
    ElMessage.warning('请先登录')
    // 跳转到登录页，并记录当前页面用于登录后返回
    router.push({
      path: '/login',
      query: { redirect: route.fullPath }
    })
    return
  }

  try {
    await request.post('/cart', {
      user_id: userStore.user.id,
      product_id: product.value.id,
      quantity: quantity.value
    })
    ElMessage.success('已加入购物车')
    
    // 询问是否去购物车
    setTimeout(() => {
      ElMessage({
        message: '是否去购物车查看？',
        type: 'success',
        duration: 3000,
        showClose: true,
        onClick: () => router.push('/cart')
      })
    }, 500)
  } catch (error) {
    ElMessage.error('加入购物车失败')
  }
}

onMounted(async () => {
  try {
    const res = await request.get(`/products/${route.params.id}`)
    product.value = res.data
  } catch (error) {
    ElMessage.error('加载商品详情失败')
  }
})
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 15px 0;
  margin-bottom: 40px;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo h1 {
  color: #E02E24;
  font-size: 24px;
}

.nav {
  display: flex;
  gap: 30px;
}

.nav a {
  text-decoration: none;
  color: #333;
}

.detail-content {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 60px;
  margin-top: 40px;
}

.product-image {
  width: 100%;
  background: #f5f5f5;
  border-radius: 8px;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: auto;
  display: block;
}

.product-details h1 {
  font-size: 32px;
  margin-bottom: 20px;
}

.price {
  font-size: 36px;
  color: #E02E24;
  font-weight: bold;
  margin-bottom: 20px;
}

.description {
  font-size: 16px;
  color: #666;
  line-height: 1.8;
  margin-bottom: 20px;
}

.stock {
  font-size: 16px;
  color: #666;
  margin-bottom: 30px;
}

.purchase-actions {
  display: flex;
  gap: 15px;
  align-items: center;
}
</style>

